
import { provide, inject } from 'vue'
import type { InjectionKey } from 'vue'
import type BaseClass from './_class-base'

export type IPerson = {
  name: string,
  age: number
}

const key = Symbol() as InjectionKey<BaseClass<IPerson> & IPerson>

// 父组件提供
export function regPerson(value: BaseClass<IPerson> & IPerson) {
  provide(key, value)
}

// 子组件获取
export function getPerson() {
  const model = inject(key) as BaseClass<IPerson> & IPerson
  return model
}

const key2 = Symbol()

// 父组件提供
export function regPerson2<T extends object>(value: BaseClass<T> & T) {
  const key = key2 as InjectionKey<BaseClass<T> & T>
  provide(key, value)
}

// 子组件获取
export function getPerson2<T extends object>() {
  const key = key2  as InjectionKey<BaseClass<T> & T>
  const model = inject(key) as BaseClass<T> & T
  return model
}